<script setup>
import { ref, reactive } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
import { size } from "lodash";
const data = reactive({
  voteList: [],
  vote: {
    userId: parseInt(localStorage.getItem("userId")),
    myOption: "",
  },
  typeList: [],
  pageIndex: 1,
  size: 6,
  total: 0,
  voteFrom: {},
});
const addVote = ref(false);
const input1 = ref();
const input2 = ref();
const input3 = ref();
const input4 = ref();
const upVote = ref(false);
const getVote = () => {
  axios
    .get("/vote/getVoteList/" + data.pageIndex + "/" + data.size)
    .then((res) => {
      data.voteList = res.data.data.voteDto;
      data.total = res.data.data.total;
    });
  axios.get("/type/getTypeList").then((res) => {
    data.typeList = res.data.data;
  });
};
const editVote = (id) => {
  axios.get("/vote/getVoteById/" + id).then((res) => {
    data.voteFrom = res.data.data;
    for (var i = 1; i <= data.voteFrom.option.length; i++) {
      if (i == 1) {
        input1.value = data.voteFrom.option[0].option;
      } else if (i == 2) {
        input2.value = data.voteFrom.option[1].option;
      } else if (i == 3) {
        input3.value = data.voteFrom.option[2].option;
      } else if (i == 4) {
        input4.value = data.voteFrom.option[3].option;
      }
    }
    upVote.value = true;
  });
};
const updateVote = () => {
  data.voteFrom.myOption =
    input1.value + "," + input2.value + "," + input3.value + "," + input4.value;
  axios.put("/vote/updateVote", data.voteFrom).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      upVote.value = false;
    }
    getVote();
    clearInput();
  });
};
const clearInput = () => {
  upVote.value = false;
  input1.value = "";
  input2.value = "";
  input3.value = "";
  input4.value = "";
};
const insertVote = () => {
  data.vote.myOption =
    input1.value + "," + input2.value + "," + input3.value + "," + input4.value;
  console.log(data.vote.myOption);
  axios.post("/vote/saveVote", data.vote).then((res) => {
    if (res.data.code == 200) {
      addVote.value = false;
      ElMessage.success("添加成功");
    }
    getVote();
    clearInput();
  });
};
const handleClose = () => {
  addVote.value = false;
};
const deleteVote = (id) => {
  axios.delete("/vote/deleteVote/" + id).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("删除成功");
    }
    getVote();
  });
};
const handleSizeChange = () => {
  getVote();
};
const handleCurrentChange = () => {
  getVote();
};
getVote();
</script>
<template>
  <div>
    <el-button style="float: right" type="primary" @click="addVote = true"
      >发布投票</el-button
    >
    <div style="margin: 10px">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item>后台管理</el-breadcrumb-item>
        <el-breadcrumb-item>投票管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-table :data="data.voteList" stripe border>
      <el-table-column type="index" label="序号" />
      <el-table-column prop="theme" label="投票主题" />
      <el-table-column prop="themeInfo" label="投票介绍" />
      <el-table-column prop="themeType" label="投票类型" />
      <el-table-column prop="num" label="投票数量" />
      <el-table-column prop="username" label="创建人" />
      <el-table-column label="选项" width="300">
        <template #default="scope">
          <el-row>
            <el-col :span="12" v-for="item in scope.row.option">
              <el-tag style="margin: 5px">{{ item.option }}</el-tag>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作" width="220">
        <template #default="scope">
          <el-tooltip
            class="box-item"
            content="编辑"
            placement="top"
            effect="light"
          >
            <el-button type="primary" @click="editVote(scope.row.id)">
              <el-icon>
                <Edit />
              </el-icon>
            </el-button>
          </el-tooltip>
          <el-tooltip
            class="box-item"
            content="删除"
            placement="top"
            effect="light"
          >
            <el-button type="danger" @click="deleteVote(scope.row.id)">
              <el-icon>
                <Delete />
              </el-icon>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        v-model:currentPage="data.pageIndex"
        v-model:page-size="data.size"
        :page-sizes="[1, 2, 4, 6]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="data.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      v-model="addVote"
      title="发布投票"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="data.vote" label-position="top">
        <el-form-item label="投票主题">
          <el-input v-model="data.vote.theme" placeholder="投票主题" />
        </el-form-item>
        <el-form-item label="投票介绍">
          <el-input
            v-model="data.vote.themeInfo"
            type="textarea"
            placeholder="投票介绍"
          />
        </el-form-item>
        <el-form-item label="投票类型">
          <el-select v-model="data.vote.themeTypeId" placeholder="投票类型">
            <el-option
              v-for="item in data.typeList"
              :label="item.type"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="选项">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input v-model="input1" placeholder="选项1" />
            </el-col>
            <el-col :span="12" style="margin-bottom: 10px">
              <el-input v-model="input2" placeholder="选项2" />
            </el-col>
            <el-col :span="12">
              <el-input v-model="input3" placeholder="选项3" />
            </el-col>
            <el-col :span="12">
              <el-input v-model="input4" placeholder="选项4" />
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addVote = false">取消</el-button>
          <el-button type="primary" @click="insertVote()">提交</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
      v-model="upVote"
      title="修改投票"
      width="30%"
      :before-close="clearInput"
    >
      <el-form :model="data.voteFrom" label-position="top">
        <el-form-item label="投票主题">
          <el-input v-model="data.voteFrom.theme" placeholder="投票主题" />
        </el-form-item>
        <el-form-item label="投票介绍">
          <el-input
            v-model="data.voteFrom.themeInfo"
            type="textarea"
            placeholder="投票介绍"
          />
        </el-form-item>
        <el-form-item label="投票类型">
          <el-select v-model="data.voteFrom.themeType" placeholder="投票类型">
            <el-option
              v-for="item in data.typeList"
              :label="item.type"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="选项">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input v-model="input1" placeholder="选项1" />
            </el-col>
            <el-col :span="12" style="margin-bottom: 10px">
              <el-input v-model="input2" placeholder="选项2" />
            </el-col>
            <el-col :span="12">
              <el-input v-model="input3" placeholder="选项3" />
            </el-col>
            <el-col :span="12">
              <el-input v-model="input4" placeholder="选项4" />
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="clearInput()">取消</el-button>
          <el-button type="primary" @click="updateVote()">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
.block {
  margin-top: 20px;
}
</style>
